/**
 * @project lauwsys
 */
package com.chimoz.lauwsys.repository.hibernate;

import java.util.List;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.chimoz.lauwsys.domain.Brand;
import com.chimoz.lauwsys.repository.BrandRepository;

/**
 * @author Christian Teguh
 * @created 12 Okt 2011 07.22.47
 */
@Repository
// To use Hibernate Transaction annotation style uncomment the code below 
// @Transactional 
public class HBrandRepository implements BrandRepository {

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session currentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public Brand saveBrand(Brand brand) {
		currentSession().saveOrUpdate(brand);
		return brand;
	}

	public void removeBrand(Brand brand) {
		currentSession().delete(brand);
	}

	
	public Brand getBrandById(Long id) {
		return (Brand) currentSession().get(Brand.class, id);
	}

	public List<Brand> listBrand(Map<String, String> criteria) {
		
		Integer offset = new Integer( criteria.get("offset") );
		Integer limit = new Integer( criteria.get("limit") );
		
		List<Brand> result = currentSession().createCriteria(Brand.class)
												.setFirstResult(offset)
												.setMaxResults(limit)
												.list();
		
		return result;
	}

}
